/**
 * Copyright (C) 2008 University of Pittsburgh
 * 
 * 
 * This file is part of Open EpiCenter
 * 
 *     Open EpiCenter is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     Open EpiCenter is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with Open EpiCenter.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 
 *   
 */
package com.hmsinc.epicenter.model.attribute;

// Generated Jan 10, 2007 11:13:09 AM by Hibernate Tools 3.2.0.beta8

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.hmsinc.epicenter.model.health.Interaction;

/**
 * AgeGroup generated by hbm2java
 */
@Entity
@Table(name = "AGE_GROUP")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@XmlRootElement(name = "agegroup", namespace = "http://epicenter.hmsinc.com/model")
@XmlType(name = "AgeGroup", namespace = "http://epicenter.hmsinc.com/model")
@XmlAccessorType(XmlAccessType.NONE)
@NamedQueries( {
		@NamedQuery(name = "getAgeGroupForAge", query = "from AgeGroup a where :age >= minAge and :age <= maxAge"),
		@NamedQuery(name = "getUnknownAgeGroup", query = "from AgeGroup a where minAge is null and maxAge is null") })
public class AgeGroup extends Attribute implements Comparable<AgeGroup> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@XmlAttribute(name = "minimumAge")
	private Integer minAge;

	@XmlAttribute(name = "maximumAge")
	private Integer maxAge;

	private Set<Interaction> interactions = new HashSet<Interaction>(0);

	// Constructors

	/** default constructor */
	public AgeGroup() {
		super();
	}

	/** full constructor */
	public AgeGroup(String name, Integer maxAge, Integer minAge, Set<Interaction> interactions) {
		super(name);
		this.maxAge = maxAge;
		this.minAge = minAge;
		this.interactions = interactions;
	}

	@Column(name = "MAX_AGE", unique = false, nullable = true, insertable = true, updatable = true, length = 3)
	public Integer getMaxAge() {
		return this.maxAge;
	}

	public void setMaxAge(Integer maxAge) {
		this.maxAge = maxAge;
	}

	@Column(name = "MIN_AGE", unique = false, nullable = true, insertable = true, updatable = true, length = 3)
	public Integer getMinAge() {
		return this.minAge;
	}

	public void setMinAge(Integer minAge) {
		this.minAge = minAge;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "ageGroup")
	public Set<Interaction> getInteractions() {
		return this.interactions;
	}

	public void setInteractions(Set<Interaction> interactions) {
		this.interactions = interactions;
	}

	@Transient
	public boolean isAgeGroupForAge(Integer age) {
		boolean r = false;
		if (this.minAge != null && this.maxAge != null && age != null && age >= minAge && age <= maxAge) {
			r = true;
		}
		return r;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Comparable#compareTo(java.lang.Object)
	 */
	public int compareTo(AgeGroup rhs) {
		return new CompareToBuilder().append(minAge, rhs.getMinAge()).toComparison();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return new HashCodeBuilder(13, 3).append(getMinAge()).append(getMaxAge()).toHashCode();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object o) {

		boolean ret = false;
		if (o instanceof AgeGroup == false) {
			ret = false;
		} else if (this == o) {
			ret = true;
		} else {
			final AgeGroup ag = (AgeGroup) o;
			ret = new EqualsBuilder().append(getMinAge(), ag.getMinAge()).append(getMaxAge(), ag.getMaxAge())
					.isEquals();
		}
		return ret;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return new ToStringBuilder(this).appendSuper(super.toString()).append("minAge", minAge)
				.append("maxAge", maxAge).toString();
	}
}
